package ca.seia.comp6521.a2.itemsetstorage;

import java.util.Arrays;

public class CharItems implements ExternilizableItems {

	private char[] items;
	
	
	public CharItems(int... items) {
		this.items = new char[items.length];
		
		int index = 0;
		for(int item: items) { 
			this.items[index] = (char) item;
			index++;
		}
	}
	
	public CharItems(char... items) {
		this.items = items;
	}

	public int getSize() {
		return this.items.length;
	}

	public char getItem(int index) {
		return this.items[index];
	}
	
	public char[] getItems() {
		return items;
	}

	public void setItems(char[] items) {
		this.items = items;
	}
	
	public static CharItems generateCandidateTriplet(CharPair pair1, CharPair pair2 ) {
		CharItems triplet = null;
		if( pair1.getValueOne() == pair2.getValueOne())  {
			if(pair1.getValueTwo() <= pair2.getValueTwo()) {
				triplet = new CharItems(pair1.getValueOne(),pair1.getValueTwo(),pair2.getValueTwo());
			} else {
				triplet = new CharItems(pair1.getValueOne(),pair2.getValueTwo(),pair1.getValueTwo());
			} 
		}
		return triplet;
	}
	
	
	public static CharItems generateCandidateItems(CharItems items1,CharItems items2, int numberOfItems) {
		int lengthItem1 = items1.getSize() - 1;
		char[] newItems = new char[numberOfItems];
		
		boolean subItemsAllEqual = true;
		for(int item1 = 0 ; item1 < lengthItem1 ; item1++) {
			if(items1.getItem(item1) != items2.getItem(item1)) {
				subItemsAllEqual = false;
				return null;
			}
			newItems[item1] = items1.getItem(item1);
		}
		if (items1.getItem(lengthItem1) <= items2.getItem(lengthItem1)) {
			newItems[lengthItem1] = items1.getItem(lengthItem1);
			newItems[lengthItem1+1] = items2.getItem(lengthItem1);
		}else {
			newItems[lengthItem1] = items2.getItem(lengthItem1);
			newItems[lengthItem1+1] = items1.getItem(lengthItem1);
		}
		return new CharItems(newItems);
	}
	

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + Arrays.hashCode(items);
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		CharItems other = (CharItems) obj;
		if (!Arrays.equals(items, other.items))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "CharItems [items=" + arraystoString(items) + "]";
	}
	
	public static String arraystoString(char[] a) {
		if (a == null)
			return "null";
		int iMax = a.length - 1;
		if (iMax == -1)
			return "[]";

		StringBuilder b = new StringBuilder();
		b.append('[');
		for (int i = 0;; i++) {
			b.append((int)a[i]);
			if (i == iMax)
				return b.append(']').toString();
			b.append(", ");
		}
	}

	//TODO maybe omit using string builder for just simple strings
	@Override
	public String toExternalFormat() {
		StringBuilder builder = new StringBuilder();
		int count = 0;
		int length = this.getItems().length;
		for( char item: this.getItems()) {
			if (count == length - 1) {
				builder.append((int)item);
			}else {
				builder.append((int)item + " ");
			}
			count ++;
		}
		return builder.toString();
	}
}

